<template>
  <div>
    <div class="user-area">
      <div class="top-area">
        <el-row>
          <el-button @click="newsModify"
                     size="small"
                     type="primary">修改</el-button>
        </el-row>
      </div>
      <div class="title">
        <div class="point"></div>
        <span>资讯详情</span>
      </div>
      <div class="news-content-area">
        <h1>{{newsTitle}}</h1>
        <div v-html="newsContent"></div>
      </div>
      <HR style="FILTER: alpha(opacity=100,finishopacity=0,style=3)"
          width="80%"
          color=#987cb9
          SIZE=3 />
      <div class="title">
        <span>评论</span>
      </div>
      <template v-if="comments && comments.length > 0">
        <el-row v-for="(user, key) in comments"
                :key="key"
                class="comment-card">
          <el-col :span="21">
            <el-col :span="1">
              <img v-if="user.head"
                   :src="user.head" />
              <img v-else
                   src="@/img/news/defaultHead.png" /></el-col>
            <el-col :span="23">
              <el-row>
                <el-col class="comment-user-name"><span>{{user.name}}</span></el-col>
                <el-col class="comment-user-comment"><span>{{user.comment}}</span></el-col>
              </el-row>
            </el-col>
          </el-col>
          <el-col :span="3">
            <el-button @click="deleted(user.commentId)"
                       type="text"
                       class="news-comment-operation-delete"
                       icon="el-icon-delete"
                       circle></el-button>
          </el-col>
        </el-row>
      </template>
      <el-row v-else>
        <div class="comment-card no-data"><span>暂无评论</span></div>
      </el-row>
    </div>
    <div class="foot">
      <el-button @click="() => {this.$router.go(-1)}"
                 type="primary"
                 plain>返回</el-button>
    </div>
  </div>
</template>

<style lang="scss">
.news-content-area {
  padding: 0 30px;
  color: #333;
}
.comment-card {
  padding: 10px 20px !important;
  margin: 5px 10px;
  border-radius: 5px;
}
.comment-card:hover {
  background-color: #f4f7fa;
  & .news-comment-operation-delete {
    display: block !important;
  }
}
.comment-user-name {
  margin-top: 5px;
  & > span {
    font-size: 14px !important;
    color: #000 !important;
    font-weight: 600;
  }
}
.comment-user-comment {
  margin-top: 14px;
  max-width: 500px;
  word-wrap: break-word;
  word-break: break-all;
  & > span {
    font-size: 14px !important;
    color: #000 !important;
    font-weight: 400;
  }
}
.news-comment-operation-delete {
  position: absolute !important;
  top: 26% !important;
  font-size: 20px !important;
  color: #ca3a3a !important;
  display: none !important;
}
.no-data {
  text-align: center;
  & > span {
    font-size: 15px;
    color: #272b42;
    line-height: 100px;
  }
}
</style>

<script>
import '@/style/info.scss'
import '@/style/common.scss'
import { HTTPSUCCESS, HTTPDATAFAIL, HTTPPARAMFAIL, HTTPUNKOWNFAIL } from '@/components/constant'
import { api } from '@/api'
import { validatenull } from '@/utils/validate'

export default {
  data() {
    return {
      comments: [],
      newsTitle: null,
      newsContent: null
    }
  },
  props: [
    'newsId'
  ],
  activated() {
    this.getNews()
    this.getComments()
  },
  methods: {
    newsModify() {
      this.$router.push({
        name: 'modifyNews',
        params: { newsId: this.newsId }
      })
    },
    deleted(id) {
      this.$http._get(api.SYS_NEWS_COMMENT_DEL, id).then(
        res => {
          if (res.code === HTTPSUCCESS) {
            this.$msg._message('操作成功', 'success')
            this.getComments()
          } else {
            this.$msg._message(res.data.msg, 'error')
          }
        }
      )
    },
    getNews() {
      this.$http._get(api.SYS_NEWS_INFO, this.newsId).then(
        res => {
          this.newsTitle = res.data.title
          this.newsContent = res.data.content
        }
      )
    },
    getComments() {
      this.$http._get(api.SYS_NEWS_COMMENTS, this.newsId).then(
        res => {
          if (res.code === HTTPSUCCESS) {
            this.comments = res.data
          } else {
            this.$msg._message(res.data.msg, 'error')
          }
        }
      )
    }
  }
}
</script>
